package com.hyzh.latte.vehicle.nezha;

import cn.hutool.core.collection.CollectionUtil;
import com.hyzh.latte.vehicle.dmo.PersonGatherDTO;
import com.hyzh.latte.vehicle.dmo.PersonTrackDTO;
import com.hyzh.latte.vehicle.dmo.entity.ParkPGEntity;
import com.hyzh.latte.vehicle.service.NezhaRecordService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author songkui
 * @since 2024/4/9 15:08
 */
@Slf4j
public class NezhaPersonManager {

    // key parkCode
    private final ConcurrentHashMap<String, NezhaPersonSchedule> personMapping = new ConcurrentHashMap<>();
    //key parkCode
    private final ConcurrentHashMap<String, List<NezhaClient>> clientMapping = new ConcurrentHashMap<>();

    private NezhaPersonManager() {
    }

    private static final class ManagerHolder {
        static final NezhaPersonManager manager = new NezhaPersonManager();
    }

    public static NezhaPersonManager getInstance() {
        return NezhaPersonManager.ManagerHolder.manager;
    }


    public void initPersonSchedule(String park, ParkPGEntity parkEntity, NezhaRecordService nezhaRecordService) {
        NezhaPersonSchedule schedule = new NezhaPersonSchedule(park, parkEntity, nezhaRecordService);
        personMapping.put(park, schedule);
        schedule.start();
    }


    public void putData(String park, List<PersonTrackDTO> dataList) {
        Double lon;
        for (PersonTrackDTO dto : dataList) {
            lon = Double.parseDouble(dto.getLon());
            if (lon < 60){
                dto.setLon(dto.getLat());
                dto.setLat(String.valueOf(lon));
            }
        }
//        log.info("**************park: {}******personTrack: {}", park, JSON.toJSONString(dataList));
        Optional.ofNullable(personMapping.get(park)).ifPresent(p -> p.putData(dataList));
    }

    public void putGatherData(String park, List<PersonGatherDTO> dataList) {
        Double lon;
        for (PersonGatherDTO dto : dataList) {
            lon = Double.parseDouble(dto.getLon());
            if (lon < 60){
                dto.setLon(dto.getLat());
                dto.setLat(String.valueOf(lon));
            }
        }
        Optional.ofNullable(personMapping.get(park)).ifPresent(p -> p.putGatherData(dataList));
    }

    public List<NezhaClient> listClientByPark(String park) {
        return clientMapping.get(park);
    }

    public void onConnect(NezhaClient client) {
        List<NezhaClient> list = clientMapping.computeIfAbsent(client.getParkCode(), k -> new ArrayList<>());
        list.add(client);
        personMapping.get(client.getParkCode()).start();
    }

    public void onCols(NezhaClient client) {
        List<NezhaClient> list = clientMapping.get(client.getParkCode());
        if (CollectionUtil.isNotEmpty(list)) {
            list.remove(client);
        }

//        if (CollectionUtil.isNotEmpty(list)){
//            personMapping.get(client.getParkCode()).stop();
//        }
    }

    public ParkPGEntity parkPGEntity(String park){
        NezhaPersonSchedule schedule = personMapping.get(park);
        return null == schedule ? null : schedule.parkPGEntity();
    }


}
